﻿using Hyx.UIComponent;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

public class UGUIDefaultControl
{
    public static GameObject CreateLoopVerticalScrollView(DefaultControls.Resources resources)
    {
        GameObject selectGo = GetselectedGo();
        if (selectGo == null)
            return null;

        GameObject go = DefaultControls.CreatePanel(resources);
        go.name = "VerticalScrollView";
        RectTransform gort = go.GetComponent<RectTransform>();
        gort.pivot = new Vector2(0.5f, 0.5f);
        gort.anchorMin = new Vector2(0.5f, 0.5f);
        gort.anchorMax = new Vector2(0.5f, 0.5f);
        gort.sizeDelta = new Vector2(500, 400);
        gort.localScale = Vector3.one;

        GameObject content = new GameObject("Content");
        RectTransform rt = content.AddComponent<RectTransform>();
        rt.pivot = new Vector2(0.5f, 1);
        rt.anchorMin = new Vector2(0.5f, 1);
        rt.anchorMax = new Vector2(0.5f, 1);
        rt.localScale = Vector3.one;
        rt.SetParent(go.transform);

        content.AddComponent<CanvasRenderer>();
        ContentSizeFitter csf = content.AddComponent<ContentSizeFitter>();
        csf.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
        csf.verticalFit = ContentSizeFitter.FitMode.PreferredSize;

        GameObject pool = new GameObject("Pool");
        RectTransform prt = pool.AddComponent<RectTransform>();
        prt.pivot = new Vector2(0.5f, 1);
        prt.anchorMin = new Vector2(0.5f, 1);
        prt.anchorMax = new Vector2(0.5f, 1);
        prt.localScale = Vector3.one;
        prt.SetParent(go.transform);
        LoopViewPool itp = pool.AddComponent<LoopViewPool>();

        LoopVerticalScrollView vertical = go.AddComponent<LoopVerticalScrollView>();
        vertical.content = rt;
        vertical.prefabPool = itp;

        return go;
    }

    public static GameObject CreateLoopHorizontalScrollView(DefaultControls.Resources resources)
    {
        GameObject selectGo = GetselectedGo();
        if (selectGo == null)
            return null;

        GameObject go = DefaultControls.CreatePanel(resources);
        go.name = "HorizontalScrollView";
        RectTransform gort = go.GetComponent<RectTransform>();
        gort.pivot = new Vector2(0.5f, 0.5f);
        gort.anchorMin = new Vector2(0.5f, 0.5f);
        gort.anchorMax = new Vector2(0.5f, 0.5f);
        gort.sizeDelta = new Vector2(500, 400);
        gort.localScale = Vector3.one;


        GameObject content = new GameObject("Content");
        RectTransform rt = content.AddComponent<RectTransform>();
        rt.pivot = new Vector2(0, 0.5f);
        rt.anchorMin = new Vector2(0, 0.5f);
        rt.anchorMax = new Vector2(0, 0.5f);
        rt.localScale = Vector3.one;
        rt.SetParent(go.transform);

        content.AddComponent<CanvasRenderer>();
        ContentSizeFitter csf = content.AddComponent<ContentSizeFitter>();
        csf.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
        csf.verticalFit = ContentSizeFitter.FitMode.PreferredSize;

        GameObject pool = new GameObject("Pool");
        RectTransform prt = pool.AddComponent<RectTransform>();
        prt.pivot = new Vector2(0.5f, 1);
        prt.anchorMin = new Vector2(0.5f, 1);
        prt.anchorMax = new Vector2(0.5f, 1);
        prt.localScale = Vector3.one;
        prt.SetParent(go.transform);
        LoopViewPool itp = pool.AddComponent<LoopViewPool>();

        LoopHorizontalScrollView horizontal = go.AddComponent<LoopHorizontalScrollView>();
        horizontal.content = rt;
        horizontal.prefabPool = itp;

        return go;
    }


    public static GameObject GetselectedGo()
    {
        GameObject selectedGo = Selection.activeGameObject;
        if (selectedGo == null)
        {
            EditorUtility.DisplayDialog("警告！", "没有选择一个正确的界面父对象", "确定");
            return null;
        }
        return selectedGo;
    }
}
